/* function Person(name) {
  this.name = name;
}

Person.prototype.say = function () {
  console.log(`hi my name is ${this.name}`);
}; */

/* class Person {
  constructor(name) {
    this.name = name;
  }

  // 实例方法
  say() {
    console.log(`hi, my name is ${this.name}`);
  }

  // 静态方法
  static create(name) {
    return new Person(name);
  }
}

class Student extends Person {
  constructor(name, number) {
    super(name);
    this.number = number;
  }

  hello() {
    super.say();
    console.log(`my school number is ${this.number}`);
  }
}

const tom = Person.create("tom");
tom.say();
const s = new Student("jack", "100");
s.hello(); */

/* function Person(name) {
  if (this instanceof Person) {
    this.name = name;
  } else {
    throw new Error("必须通过new关键词来调用Person");
  }
} */

function Person(name) {
  console.log("🚀 ~ Person ~ new.target:", new.target);
  if (typeof new.target !== "undefined") {
    this.name = name;
  } else {
    throw new Error("必须通过new关键词来调用Person");
  }
}
const person = new Person("why");
const notPerson = Person.call(person, "why"); // 抛出错误
